package crafex.engine.tools;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class TextField {
	
	private Bitmap letters;
	private IntVertex2D grid;
	private IntVertex2D bounds;
	private String text;
	
	public TextField(Bitmap letters, IntVertex2D grid, IntVertex2D bounds, String text){
		this.letters = letters;
		this.grid = grid;
		this.bounds = bounds;
		this.text = text;
	}
	
	public void setText(String text){
		this.text = text;
	}
	
	public String getText(){
		return text;
	}
	
	public Bitmap getField(){
		char[] newtext = text.toCharArray();
		Bitmap textmap = Bitmap.createBitmap(bounds.getX(), bounds.getY(), letters.getConfig());
		Canvas textcanvas = new Canvas(textmap);
		for(int i = 0, x = 0, y = 0; i < newtext.length; i++, x++){
			int carx = 0;
			int cary = 0;
			
			for(char tester = 'a'; tester != newtext[i]; tester++){
				if(carx < grid.getX() - 1){
					carx++;
				}else{
					carx = 0;
					cary++;
				}
			}
			if((x * (letters.getWidth() / grid.getX())) + (letters.getWidth() / grid.getX()) > bounds.getX()){
				x = 0;
				y++;
			}

			int srcx = (letters.getWidth() / grid.getX()) * carx;
			int srcy = (letters.getHeight() / grid.getY()) * cary;
			int dstx = (letters.getWidth() / grid.getX()) * x;
			int dsty = (letters.getHeight() / grid.getY()) * y;
			textcanvas.drawBitmap(letters, new Rect(srcx, srcy, srcx + (letters.getWidth() / grid.getX()), srcy + (letters.getHeight() / grid.getY())),
					new Rect(dstx, dsty, dstx + (letters.getWidth() / grid.getX()), dsty + (letters.getHeight() / grid.getY())), null);
		}
		return textmap;
	}
	
}
